package com.tansun.easycare.rule.referencedata.service.impl;

import java.util.Collection;
import java.util.List;

import org.springframework.stereotype.Service;

import com.tansun.easycare.rule.common.DataCache;
import com.tansun.easycare.rule.common.service.impl.RuleBaseServiceImpl;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.referencedata.dao.RefFuncParamDao;
import com.tansun.easycare.rule.referencedata.entity.RefFuncParam;
import com.tansun.easycare.rule.referencedata.entity.RuleReferenceData;
import com.tansun.easycare.rule.referencedata.service.RefFuncParamService;

@Service
public class RefFuncParamServiceImpl extends RuleBaseServiceImpl<RefFuncParamDao, RefFuncParam> implements RefFuncParamService {

	@Override
	public void saveOrUpdate(RefFuncParam refFuncParam) throws BizException {
		List<RefFuncParam>  rfList = this.findList(refFuncParam);
		if(rfList!=null && rfList.size()>0) {
			refFuncParam.setId(rfList.get(0).getId());
			this.dao.update(refFuncParam);
		}else {
			this.save(refFuncParam);
		}
		DataCache.refFuncParamChange(refFuncParam.getProjectCode());
	}

	public Collection<RuleReferenceData> findReferenceDataList(RefFuncParam refFuncParam) throws BizException{
		return this.dao.findReferenceDataList(refFuncParam);
	}

	public Collection<RuleReferenceData> findAllFuncReferenceDataList(RefFuncParam refFuncParam) throws BizException{
		return this.dao.findAllFuncReferenceDataList(refFuncParam);
	}
	
	@SuppressWarnings("unchecked")
	public Collection<RuleReferenceData> findAllFuncReferenceDataListByprojectCode(String projectCode) throws BizException{
		Collection<RuleReferenceData> resultColl = null;
	    String cacheKey = DataCache.getCacheKey(projectCode, DataCache.REFFUNCPARAM_OPTION);
	    if(DataCache.canGetFromCache(cacheKey) &&1==2) {
	    	resultColl = (Collection<RuleReferenceData>) DataCache.getValue(cacheKey);
	    }else {
		    RefFuncParam refFuncParam = new RefFuncParam();
		    refFuncParam.setProjectCode(projectCode);
		    resultColl = this.findAllFuncReferenceDataList(refFuncParam);
		    DataCache.setValue(cacheKey, resultColl);
	    }
	    return resultColl;
	}
}
